package com.week.tiny_video.controller;

import com.week.tiny_video.pojo.History;
import com.week.tiny_video.pojo.User;
import com.week.tiny_video.result.Result;
import com.week.tiny_video.service.HistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
@CrossOrigin(allowCredentials = "true")
@RestController
public class HistoryController {

    @Autowired
    private HistoryService historyService;

    //历史记录列表
    @GetMapping("history")
    public Result list(HttpSession session){
        User user = (User) session.getAttribute("user");
        List<History> list = historyService.list(user.getId());

        HashMap<String,Object> map = new HashMap<>();
        map.put("list",list);
        return new Result(200,"历史记录",true,map);
    }

    //添加到历史记录表
    @PostMapping("history")
    public Result add(@RequestBody History history,
                      HttpSession session){
        User user = (User) session.getAttribute("user");
        history.setTime(new Date());
        history.setUser(user);
        historyService.add(history);
        return new Result(200,"添加到观看历史记录",true);
    }

    @DeleteMapping("delete/{id}")
    public Result delete(@PathVariable("id") Integer id){
        historyService.delete(id);
        return new Result(200,"删除历史记录",true);
    }
}
